package deom6;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2023-04-09
 * Time: 7:03
 */
class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}
public class Test1 {
    public ListNode EntryNodeOfLoop(ListNode pHead){
        ListNode slow = pHead;
        ListNode fast = pHead;
        //先判断是否有环
        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            //相等证明有环
            if(fast == slow){
                slow = pHead;
                while (fast != slow){
                     fast = fast.next;
                     slow = slow.next;
                }
                //再次相遇就到入口
                return fast;
            }
        }
        //无环
        return null;
    }
}
